/*
 * Copyright 2016-present Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onosproject.bmv2.api.runtime;

import com.google.common.annotations.Beta;

/**
 * General exception of the BMv2 runtime APIs.
 */
@Beta
public final class Bmv2RuntimeException extends Exception {

    private final Code code;
    private String codeString;

    public Bmv2RuntimeException(String message) {
        super(message);
        this.code = Code.OTHER;
        this.codeString = message;
    }

    public Bmv2RuntimeException(Throwable cause) {
        super(cause);
        this.code = Code.OTHER;
        this.codeString = cause.toString();
    }

    public Bmv2RuntimeException(Code code) {
        super(code.name());
        this.code = code;
    }

    public Code getCode() {
        return this.code;
    }

    public String explain() {
        return (codeString == null) ? code.name() : code.name() + " " + codeString;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + " " + explain();
    }

    public enum Code {
        TABLE_FULL,
        TABLE_INVALID_HANDLE,
        TABLE_EXPIRED_HANDLE,
        TABLE_COUNTERS_DISABLED,
        TABLE_METERS_DISABLED,
        TABLE_AGEING_DISABLED,
        TABLE_INVALID_TABLE_NAME,
        TABLE_INVALID_ACTION_NAME,
        TABLE_WRONG_TABLE_TYPE,
        TABLE_INVALID_MBR_HANDLE,
        TABLE_MBR_STILL_USED,
        TABLE_MBR_ALREADY_IN_GRP,
        TABLE_MBR_NOT_IN_GRP,
        TABLE_INVALID_GRP_HANDLE,
        TABLE_GRP_STILL_USED,
        TABLE_EMPTY_GRP,
        TABLE_DUPLICATE_ENTRY,
        TABLE_BAD_MATCH_KEY,
        TABLE_INVALID_METER_OPERATION,
        TABLE_DEFAULT_ACTION_IS_CONST,
        TABLE_DEFAULT_ENTRY_IS_CONST,
        TABLE_GENERAL_ERROR,
        TABLE_UNKNOWN_ERROR,

        DEV_MGR_ERROR_GENERAL,
        DEV_MGR_UNKNOWN,

        COUNTER_INVALID_NAME,
        COUNTER_INVALID_INDEX,
        COUNTER_ERROR_GENERAL,
        COUNTER_ERROR_UNKNOWN,

        SWAP_CONFIG_DISABLED,
        SWAP_ONGOING,
        SWAP_NO_ONGOING,
        SWAP_ERROR_UKNOWN,

        // TODO: add other codes based on autogenerated Thrift APIs

        OTHER
    }
}
